//
//  TaxesCalculator.m
//  Rong360
//
//  Created by tianliwei on 16/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "TaxesCalculator.h"
#import "Config.h"
#import "TaxesSecondHisUnit.h"
#import "TaxesNewHisUnit.h"
@implementation TaxesCalculator

- (void)beginCalculateWithHouseSecondHand{
    
    CGFloat price_second = [self.totalPrice_second floatValue] * 10000.0;
    CGFloat origin = [self.orginPrice floatValue] * 10000.0;
    if ([self.square_second floatValue] > 120) {
        self.houseType = UnnormalType;
    }
    
    //二手房 契税
    CGFloat qs_2nd = 0;//二手房 契税
    
    if (self.houseType != UnnormalType) {
        if (self.firstBuy) {
            if ([self.square_second floatValue] < 90.0) {
                NSString *qishui_1dang = [Config shareConfig].qishui_1dang;
                self.qishui_cur = [Config shareConfig].qishui_1dang;
                CGFloat qs_1 = [qishui_1dang floatValue]/ 100;
                qs_2nd = qs_1 * price_second ;
            }else if ([self.square_second floatValue] > 90.0 && [self.square_second floatValue] < 140){
                CGFloat qs_2 = [[Config shareConfig].qishui_2dang floatValue] / 100;
                self.qishui_cur = [Config shareConfig].qishui_2dang;
                qs_2nd = qs_2 *price_second;
            }
        }
    }else{
        CGFloat qs_3 = [[Config shareConfig].qishui_3dang floatValue];
        self.qishui_cur = [Config shareConfig].qishui_3dang;
        qs_2nd = qs_3 *price_second / 100;
    }
    self.qishui_2nd = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)qs_2nd]];
    //营业税及附加
    CGFloat yyfj = 0;
    
    if (self.houseType == EcomicalType) {
        yyfj = 0;
    }else{
        CGFloat chengjian = [[Config shareConfig].chenjian floatValue] / 100;
        CGFloat jiaoyufujia = [[Config shareConfig].jiaoyufujia floatValue] / 100;
        CGFloat yingyeshui = [[Config shareConfig].yingyeshui floatValue] / 100;
        
        if (self.overFive) {
            if (self.houseType == NormalType) {
                yyfj = 0;
            }else if (self.houseType == UnnormalType){
                if (self.markType == TotalType) {
                    yyfj = (price_second - 0) * yingyeshui * (1 + chengjian + jiaoyufujia);
                }else{
                    yyfj = (price_second - origin) * yingyeshui * (1 + chengjian + jiaoyufujia);
                }
            }
        }else{
            yyfj = price_second * yingyeshui * (1 + chengjian + jiaoyufujia);
        }
    }
    self.yingyefujia = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)yyfj]];
    
    //个人所得税
    CGFloat geren;
    
    self.geren_cur = @"0";
    
    if (self.overFive && self.onlyOne) {
        geren = 0;
    }else{
        if (self.markType == TotalType) {
            geren = price_second * [[Config shareConfig].geren_quan floatValue] / 100;
            self.geren_cur = [Config shareConfig].geren_quan;
        }else{
            geren = (price_second - origin) * [[Config shareConfig].geren_cha floatValue] / 100;
            self.geren_cur = [Config shareConfig].geren_cha;
        }
    }
    self.geren = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)geren]];
    
    //印花税
    CGFloat yinhua = price_second * [[Config shareConfig].yinghua floatValue] / 100;
    self.yinhua = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)yinhua]];
    
    //综合地价税
    CGFloat zonghedijia;
    if (self.houseType == EcomicalType) {
        if (self.markType == TotalType) {
            zonghedijia = price_second * [[Config shareConfig].dijiakuan_quan floatValue] / 100;
            self.zonghedijia_cur = [Config shareConfig].dijiakuan_quan;
        }else{
            zonghedijia = (price_second - origin) * [[Config shareConfig].dijiakuan_cha floatValue] / 100;
            self.zonghedijia_cur = [Config shareConfig].dijiakuan_cha;
        }
    }else{
        zonghedijia = 0;
        self.zonghedijia_cur = @"0";
    }
    self.zonghedijia = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)zonghedijia]];
    
    //中介费
    CGFloat zhongjie = price_second * [[Config shareConfig].zhongjie floatValue] / 100;
    self.zhongjie = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)zhongjie]];
    
    //合计
    CGFloat heji = qs_2nd + yinhua + geren + yyfj + zonghedijia + zhongjie;
    self.heji = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)heji]];
    
    
    
    self.results = @[self.heji,self.qishui_2nd,self.yingyefujia,self.geren,self.yinhua,self.zonghedijia,self.zhongjie];

    TaxesSecondHisUnit *taxesSecondUnit = [[TaxesSecondHisUnit alloc]init];
    taxesSecondUnit.square = self.square_second;
    taxesSecondUnit.price = self.totalPrice_second;
    taxesSecondUnit.totaltaxes = self.heji;
    taxesSecondUnit.overfive = self.overFive;
    taxesSecondUnit.onlyone = self.onlyOne;
    //保存数据库
    if ([self.heji floatValue]) {
        DBMan* dbm = [DBMan shareDB];
        [dbm saveToSecondTaxesHis:taxesSecondUnit];
    }
}

- (void)beginCalculateWithHouseNew{
    //新房
    CGFloat price_new = [self.totalPrice_new floatValue] * 10000.0;

    CGFloat qs_new;//新房契税
    if ([self.square_new floatValue] < 90) {
        CGFloat qs_1 = [[Config shareConfig].qishui_1dang floatValue] / 100.0;
        self.qishui_cur = [Config shareConfig].qishui_1dang;
        qs_new = qs_1 * price_new;
    }else if ([self.square_new floatValue] > 90 && [self.square_new floatValue] < 140){
        CGFloat qs_2 = [[Config shareConfig].qishui_2dang floatValue] / 100.0;
        self.qishui_cur = [Config shareConfig].qishui_2dang;
        qs_new = qs_2 * price_new;
    }else{
        
        CGFloat qs_3 = [[Config shareConfig].qishui_3dang floatValue] / 100.0;
        self.qishui_cur = [Config shareConfig].qishui_3dang;
        qs_new = qs_3 * price_new;
    }
    self.qishui_new = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)qs_new]];
    //合计
    CGFloat heji = qs_new;
    
    self.heji = [DigitalUtil separatedDigitStringWithStr:[NSString stringWithFormat:@"%d",(int)heji]];
    self.results = @[self.heji,self.qishui_new,@"0",@"0",@"0",@"0",@"0"];
    TaxesNewHisUnit *taxesNewUnit = [[TaxesNewHisUnit alloc]init];
    taxesNewUnit.price = self.totalPrice_new;
    taxesNewUnit.square = self.square_new;
    taxesNewUnit.totaltaxes = self.heji;
    if ([self.heji floatValue]) {
        //保存数据库
        DBMan* dbm = [DBMan shareDB];
        [dbm saveToNewTaxesHis:taxesNewUnit];
    }
}

@end
